#{extends '/template/main.html' /}
<!-- Removing search and results count filter -->
<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">全部订单</h3>
    </div>
    <div class="panel-body">
        <form method="get" id="searchForm" action="/BS0017/admin/order/index/${pageNumber}">
            <div style="margin-bottom: 20px;width:160px;float: left;">
                <input type="text" name="orderNumber" id="orderNumber" class="form-control" style="width:150px" placeholder="订单编号" value="${orderNumber}">
            </div>
            <div style="margin-bottom: 20px;width:120px;float: left;margin-right: 10px;">
                <input type="text" class="form-control datepicker" data-format="yyyy-mm-dd" name="startTime" style="width: 120px" placeholder="开始时间" value="${startTime}">
            </div>
            <div style="margin-bottom: 20px;width:120px;float: left;margin-right: 10px;">
                <input type="text" class="form-control datepicker" data-format="yyyy-mm-dd" name="endTime" style="width: 120px" placeholder="结束时间" value="${endTime}">
            </div>
            <div style="margin-bottom: 20px;width:120px;float: left;margin-right: 10px;">
                <select name="status" class="form-control" style="width: 120px">
                    <option value="" ${status == null ? 'selected' : ''}>全部支付状态</option>
                    <option value="UNPAID" ${status.toString() == 'UNPAID' ? 'selected' : ''}>未付款</option>
                    <option value="PAID" ${status.toString() == 'PAID' ? 'selected' : ''}>已付款</option>
                </select>
            </div>
        </form>
        <button class="btn btn-turquoise" id="searchBtn">开始检索</button>
        <button class="btn btn-turquoise" onclick="exportExcel()">导出</button>
        <button class="btn btn-turquoise" onclick="printOrders()">打印订单</button>

        <table class="table table-bordered table-striped" id="example-2">
            <thead>
            <tr>
                <th class="no-sorting">
                    <input type="checkbox" name="checkAll"  >
                </th>
                <th>订单编号</th>
                <th>下单人</th>
                <th>下单时间</th>
                <th>金额(元)</th>
                <th>支付状态</th>
                <th>操作</th>
            </tr>
            </thead>

            <tbody class="middle-align">
            #{paginate.list items:resultPage , as :'order'}
            <tr>
                <td>
                    <input type="checkbox" orderId="${order?.id}" name="subBox" >
                </td>
                <td>${order?.orderNumber}</td>
                <td>${order?.webUser?.nickName}</td>
                <td>${order?.createdAt?.format('MM-dd HH:mm')}</td>
                <td>${order?.amount?.format('#.##')}</td>
                <td>&{'OrderStatus.' + order?.status}</td>
                <td>
                    <a href="/BS0023/admin/order/detail/${order?.id}/${pageNumber}" class="btn btn-secondary btn-sm btn-icon icon-left">
                        查看
                    </a>


                </td>
            </tr>
            #{/paginate.list}
            </tbody>
        </table>
        <div id="layerPage" class="col-xs-12"></div>
    </div>
</div>



<!-- Imported styles on this page -->
<link rel="stylesheet" href="/public/js/datatables/dataTables.bootstrap.css">

<script src="/public/js/datatables/js/jquery.dataTables.min.js"></script>


<!-- Imported scripts on this page -->
<script src="/public/js/datatables/dataTables.bootstrap.js"></script>
<script src="/public/js/datatables/yadcf/jquery.dataTables.yadcf.js"></script>
<script src="/public/js/datatables/tabletools/dataTables.tableTools.min.js"></script>

<script type="text/javascript" src="/public/laypage/laypage.js"></script>
<script type="text/javascript">
    var orderType = '${orderType}';
    var frozenGoodsType = '${frozenGoodsType}';
    var orderIds = '';
    $(document).ready(function(){
        // 提示
        var msg = '${flash.get("msg")}';
        if(msg) {

        }

        // 分页
        laypage({
            cont: $('#layerPage'), //容器。值支持id名、原生dom对象，jquery对象,
            pages: '${resultPage?.pageCount}', //总页数
            skip: true, //是否开启跳页
            skin: 'yahei',
            curr:'${pageNumber}',
            jump: function(e, first){ //触发分页后的回调
                if(!first){ //一定要加此判断，否则初始时会无限刷新
                    $('#pageNumber').val(e.curr);
                    $("#searchForm").attr("action", "/BS0017/admin/order/index/"+e.curr);
                    $('#searchForm').submit();
                }
            }
        });
        $(document).on("click", "#searchBtn", function(){
            $("#searchForm").attr("action", "/BS0017/admin/order/index/1");
            $('#searchForm').submit();
        });

        //全选/全不选
        $("input[name='checkAll']").click(function() {
            var flag = $(this).prop("checked");
            if(flag == 'undefined' || flag == null || flag == '') flag = false;
            $("input[name='checkAll']").prop("checked", flag);
            $("[name=subBox]:checkbox").each(function() {
                $(this).prop("checked", flag);
            })
            orderIds = updateCheckIds();
            console.log(orderIds);

        });

        //单选
        $("input[name='subBox']").click(function () {

            var flag = $(this).prop("checked");
            if(flag == undefined || flag == null || flag == '') flag = false;
            $(this).prop('checked',flag);
            orderIds = updateCheckIds();
            console.log(orderIds);
        })
    });


    //点击确认删除 之后执行的方法
    function sureDeleted() {
        //获取删除的id
        var id = $('#deleteId').val();
        document.location.href='/BS0017/admin/order/delete/'+id+'/${pageNumber}';
    }

    //导出
    function exportExcel() {
        var url = '/BS0017/admin/order/exportExcel';
        if(frozenGoodsType != null && frozenGoodsType != '')
            url += '/' + orderType + '/' + frozenGoodsType;
//        alert(url);
        $("#searchForm").attr("action", url);
        $('#searchForm').submit();
    }

    //更新选择的订单ID
    function updateCheckIds() {
        var checkIds = '' ;
        var allcheck = 'checked';
        var count = 0;
        $('input[name="subBox"]').each(function() {
            var check = $(this).prop("checked");
            if(check){
                count += 1 ;
                checkIds += $(this).attr('orderId') + ',';
            }else {
                allcheck = false ;
            }

        });

        $("input[name='checkAll']").prop("checked", allcheck);
        return checkIds ;
    }
    //打印
    function printOrders() {
//        alert(orderIds);
        if(orderIds.length == 0 ){
            layer.alert("请选择要打印的订单",{icon:0 , title:'系统提示'});
            return false ;
        }
        window.open('/BS0017/admin/order/print?orderIds='+orderIds);
    }

</script>


